package com.xjrh.traintravel.collector.controller;

import com.xjrh.traintravel.collector.common.ResultData;
import com.xjrh.traintravel.collector.runner.NettyClientManager;
import io.netty.channel.Channel;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 客户端连接管理Controller
 *  <p>
 *      查看和管理已连接的客户端信息
 *  </p>
 *
 * @author snps
 * @create 2022-01-17 13:54
 * @describe
 **/
@RequestMapping("/connect/manage")
@RestController
@Slf4j
public class ConnectManageController {

    @Resource
    private NettyClientManager nettyClientManager;


    @GetMapping("/getClients")
    public ResultData getClients() {
        ResultData result = new ResultData(Boolean.TRUE);
        result.setData(nettyClientManager.getClientChannelMap());
        return result;
    }


    @PostMapping("/getClientsShow")
    public ResultData getClientsShow() {
        ResultData result = new ResultData(Boolean.TRUE);

        Map<String, Channel> mapClients = nettyClientManager.getClientChannelMap();
        if (mapClients == null || mapClients.size() == 0) {
            return result;
        }

        List<ClientVo> clients = new ArrayList<>(mapClients.size());
        mapClients.forEach((key, value) -> {
            clients.add(new ClientVo(key, "", "1.0"));
        });
        result.setData(clients);
        return result;
    }

    @Data
    class ClientVo {
        private String macAddr;
        private String description;
        private String version;

        public ClientVo() {}

        public ClientVo(String macAddr, String description, String version) {
            this.macAddr = macAddr;
            this.description = description;
            this.version = version;
        }
    }

}